package com.snail.snailvr.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.provider.MediaStore;

import com.snail.snailvr.VRApp;
import com.snail.snailvr.player.PlayActivity;

/**
 * 将通用SharedPreferences 存取都由此类处理
 */
public class SharedPreferencesUtil {

    private static SharedPreferencesUtil sharedPreferencesUtil;
    private static final String PREFERENCE_NAME = "snail_vr";
    private static SharedPreferences preferences;

    public static SharedPreferencesUtil getInstance() {
        return getInstance(VRApp.getContext());
    }
    public static SharedPreferencesUtil getInstance(Context context) {
        if (sharedPreferencesUtil == null) {
            sharedPreferencesUtil = new SharedPreferencesUtil();
            preferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
        }
        return sharedPreferencesUtil;
    }

    public Boolean getIsFirst() {
        return preferences.getBoolean(Constant.KEY_IS_FIRST, true);
    }

    public void setIsFirst(Boolean isFirst) {
        preferences.edit().putBoolean(Constant.KEY_IS_FIRST, isFirst).apply();
    }

    public void setSortOrder(String sortOrder) {
        preferences.edit().putString(Constant.KEY_SORT_ORDER, sortOrder).apply();
    }

    public String getSortOrder() {
        return preferences.getString(Constant.KEY_SORT_ORDER, MediaStore.Video.Media.SIZE);
    }

    public boolean isWifiOnly() {
        return false;
    }

    public void isWifiOnly(boolean isWifiOnly) {
        preferences.edit().putBoolean(Constant.KEY_WIFI_ONLY, isWifiOnly).apply();
    }

    public String getDropboxToken() {
        return preferences.getString(Constant.KEY_DROPBOX_TOKEN, null);
    }

    public void saveDropboxToken(String accessToken) {
        preferences.edit().putString(Constant.KEY_DROPBOX_TOKEN, accessToken).apply();
    }

    public void removeDropboxToken() {
        preferences.edit().remove(Constant.KEY_DROPBOX_TOKEN).apply();

    }

    //播放器模式
    public int getGlassState(){
        return preferences.getInt(Constant.KEY_GALSS_STATE, Constant.GLASS_ON);
    }
    public void setGlassState(int state) {
        preferences.edit().putInt(Constant.KEY_GALSS_STATE, state).apply();
    }

    public String getChannelID() {
        return preferences.getString(Constant.KEY_CHANNEL_ID,"");
    }

    public void setChannelID(String channelID) {
        preferences.edit().putString(Constant .KEY_CHANNEL_ID, channelID).apply();
    }

    public long getVersionUpdateDownloadID(){
        return preferences.getLong(Constant.VERSION_UPDATE_DOWNLOAD_ID, -1l);
    }

    public void setVersionUpdateDownloadID(long ID){
        preferences.edit().putLong(Constant.VERSION_UPDATE_DOWNLOAD_ID, ID).apply();
    }

    public void setStartTime(){
        preferences.edit().putLong(Constant.KEY_START_TIME, System.currentTimeMillis()).apply();
    }

    public long getStartTime(){
        return preferences.getLong(Constant.KEY_START_TIME, 0L);
    }

    public void setPlayModel(int playModel) {
        preferences.edit().putInt(Constant.KEY_PLAY_MODEL, playModel).apply();
    }
    public int getPlayModel(){
        return preferences.getInt(Constant.KEY_PLAY_MODEL, PlayActivity.TYPE_360_2D);
    }
    public void setDownloadIdOutDownloadManager(long downloadId){
        preferences.edit().putLong(Constant.KEY_DOWNLOAD_ID_OUT_DOWNLOAD_MANAGER,downloadId).apply();
    }
    public long getDownloadIdOutDownloadManager(){
        long download=preferences.getLong(Constant.KEY_DOWNLOAD_ID_OUT_DOWNLOAD_MANAGER, -1)-1;
        setDownloadIdOutDownloadManager(download);
        return download;//获取从WiFi传输或者网盘下载的最新的Id 全是负数
    }

    public void databaseUpdated(){
        preferences.edit().putBoolean(Constant.KEY_UPDATE_DATABASE,true).apply();
    }
    public boolean isDatabaseUpdated() {
        return preferences.getBoolean(Constant.KEY_UPDATE_DATABASE,false);
    }
}

